package scau.jump.securitytrace.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import scau.jump.securitytrace.init.Init;
import scau.jump.securitytrace.popupwindow.ProductWindow;

import com.securitytrace.model.Product;

public class UpdateProductButtonListener implements ActionListener {

	private ProductWindow productWindow;
	private Product product;
	
	public UpdateProductButtonListener(ProductWindow productWindow) {
		this.productWindow = productWindow;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String name = productWindow.getProductTextField().getText();
		String desc = productWindow.getProductTextArea().getText();
		if(name.trim().isEmpty()) {
			JOptionPane.showMessageDialog(productWindow, "产品类别不能为空");
		}else if(desc.trim().isEmpty()) {
			JOptionPane.showMessageDialog(productWindow, "产品类别描述不能为空");
		}else {
			product.setName(name);
			product.setOtherInfo(desc);
			try {
				updateProductToDataBase(product);
				productWindow.getManagerClient().setResultProduct(product);
				productWindow.dispose();
			} catch(Exception ex) {
				JOptionPane.showMessageDialog(productWindow, "操作失败");
			}
		}
	}


	private void updateProductToDataBase(Product product) {
		Init.getProductService().update(product);
		System.out.println("update product");
	}

	/***以下为get/set方法***/
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
}
